{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3a79ef54-63e6-429f-ba9f-590b736a7f29",
   "metadata": {},
   "source": [
    "## 2.9 获取张量的信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "- 创建形状为(2,3,4)的随机张量re1\n",
    "- 获取包含re1形状信息的张量re2\n",
    "- 获取包含re1中元素个数的张量re3\n",
    "- 获取包含re1维度信息的张量re4"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8d7baa9c-93a2-42f3-a3c1-231cdb587f2d",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74ad989a-9b82-43e1-b841-e74284cd5936",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "使用tf.shape方法返回一个包含指定张量形状信息的张量。\n",
    "\n",
    "使用tf.size方法返回一个包含指定张量中元素个数的张量。\n",
    "\n",
    "使用tf.rank方法返回一个包含指定张量维度信息的张量。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[[ 0.49555212  1.3872551  -0.34560046 -0.92312914]\n",
      "  [-1.1562473  -0.4403601  -0.26783615  1.0596397 ]\n",
      "  [ 1.0903798  -1.0336822  -1.2646819   0.51710355]]\n",
      "\n",
      " [[-0.1494673   0.2755813   0.9570926  -0.26227564]\n",
      "  [ 0.57211727 -1.370996   -0.7895704   0.83134294]\n",
      "  [-1.1452352   0.6874688  -0.8033963  -1.726562  ]]], shape=(2, 3, 4), dtype=float32)\n",
      "tf.Tensor([2 3 4], shape=(3,), dtype=int32)\n",
      "tf.Tensor(24, shape=(), dtype=int32)\n",
      "tf.Tensor(3, shape=(), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "# 创建形状为(2,3,4)的张量rel\n",
    "re1=tf.random.normal(shape=(2,3,4))\n",
    "print(re1)\n",
    "\n",
    "# 返回包含re1形状信息的张量\n",
    "re2=tf.shape(re1)\n",
    "print(re2)\n",
    "\n",
    "# 返回包含re1中元素个数信息的张量\n",
    "re3=tf.size(re1)\n",
    "print(re3)\n",
    "\n",
    "# 返回包含re1维度信息的张量\n",
    "re4=tf.rank(re1)\n",
    "print(re4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "745e27eb-80a5-43fe-acba-d5ddd5fa4de9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
